package com.mobiletrain.org.motherkitch.sliding;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.internal.app.ToolbarActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobiletrain.org.motherkitch.R;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;

public class PhotoActivity extends ActionBarActivity {

    private Toolbar toolbar;
    private LinearLayout photo;
    private File mFile;
    private LinearLayout preDate;
    private LinearLayout babySex;
    private LinearLayout babyBirthday;
    private int visibility;
    private LinearLayout name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFile = new File(getExternalCacheDir(), "aaa.png");
        setContentView(R.layout.activity_photo);
        name = (LinearLayout) findViewById(R.id.name);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        babyBirthday = (LinearLayout) findViewById(R.id.babyBirthday);
        preDate = (LinearLayout) findViewById(R.id.preDate);
        babySex = (LinearLayout) findViewById(R.id.babySex);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        photo = (LinearLayout) findViewById(R.id.photo);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPhoto();


            }
        });

        getUserInfo();
    }

    private void getUserInfo() {
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog();
            }
        });

        final LinearLayout birthday = (LinearLayout) findViewById(R.id.birthday);
        birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this)
                        .setItems(new String[]{"已出生", "怀孕中", "备孕中"}, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:

                                        visibility = babyBirthday.getVisibility();
                                        if(visibility == View.GONE){
                                            babyBirthday.setVisibility(View.VISIBLE);
                                            babySex.setVisibility(View.VISIBLE);
                                        }

                                        babyBirthday.setVisibility(View.VISIBLE);
                                        babyBirthday.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                DatePickerDialog datePickerDialog = new DatePickerDialog(PhotoActivity.this, new DatePickerDialog.OnDateSetListener() {
                                                    @Override
                                                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                                        TextView birthday = (TextView) babyBirthday.findViewById(R.id.date);
                                                        birthday.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                                                    }
                                                }, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);
                                                datePickerDialog.setTitle("选择宝宝生日");
                                                datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {

                                                    }
                                                });
                                                datePickerDialog.show();
                                            }
                                        });

                                        babySex.setVisibility(View.VISIBLE);
                                        babySex.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                AlertDialog.Builder builder1 = new AlertDialog.Builder(PhotoActivity.this)
                                                        .setItems(new String[]{"男", "女"}, new DialogInterface.OnClickListener() {

                                                            @Override
                                                            public void onClick(DialogInterface dialog, int which) {
                                                                switch (which) {
                                                                    case 0:
                                                                        TextView sex = (TextView) findViewById(R.id.sex);
                                                                        sex.setText("男");
                                                                        break;
                                                                    case 1:
                                                                        TextView sex1 = (TextView) findViewById(R.id.sex);
                                                                        sex1.setText(" 女");
                                                                        break;
                                                                }
                                                            }
                                                        });
                                                builder1.show();
                                            }
                                        });
                                        break;
                                    case 1:
                                        visibility = babyBirthday.getVisibility();
                                        if(visibility == View.VISIBLE){
                                            babyBirthday.setVisibility(View.GONE);
                                            babySex.setVisibility(View.GONE);
                                        }
                                        if(preDate.getVisibility()==View.GONE){
                                            preDate.setVisibility(View.VISIBLE);
                                        }

                                        preDate.setVisibility(View.VISIBLE);

                                        final TextView babyPreDate = (TextView) findViewById(R.id.babyPreDate);
                                        preDate.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                DatePickerDialog datePickerDialog = new DatePickerDialog(PhotoActivity.this, new DatePickerDialog.OnDateSetListener() {
                                                    @Override
                                                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                                        babyPreDate.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
                                                    }
                                                }, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);
                                                datePickerDialog.show();
                                            }

                                        });

                                        break;
                                    case 2:
                                        babySex.setVisibility(View.GONE);
                                        babyBirthday.setVisibility(View.GONE);
                                        preDate.setVisibility(View.GONE);
                                        break;
                                }
                            }
                        });
                builder.show();
            }
        });
    }

    private void getDialog() {
        View view = LayoutInflater.from(this).inflate(R.layout.addfood_item, null);
        final EditText add_content = (EditText) view.findViewById(R.id.add_content);
        TextView add_cancel = (TextView) view.findViewById(R.id.add_cancel);
        TextView add_confirm = (TextView) view.findViewById(R.id.add_confirm);
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        final AlertDialog show = builder.setView(view).show();
        show.setCancelable(false);
        final TextView tv_name = (TextView) findViewById(R.id.tv_name);
        tv_name.setHint(add_content.getText().toString());
        add_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_name.setText(add_content.getText().toString());
                show.dismiss();
            }
        });
        add_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.dismiss();
            }
        });
    }

    public void getPhoto() {
      /* // photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {*/
        AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
        builder.setItems(new String[]{"从相册选取", "拍照获取"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
//                                Intent i = new Intent(Intent.ACTION_PICK,
//                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                                //调用android的图库
                        Intent i = new Intent();
                        i.addCategory(Intent.CATEGORY_OPENABLE);
                        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
                        i.setType("image/*");
                        //根据版本号不同使用不同的Action
                        if (Build.VERSION.SDK_INT < 19) {
                            i.setAction(Intent.ACTION_GET_CONTENT);
                        } else {
                            i.setAction(Intent.ACTION_OPEN_DOCUMENT);
                        }
                        startActivityForResult(i, 1);


                        break;

                    case 1:
                        Intent i2 = new Intent();
                        i2.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

                        i2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));


                        startActivityForResult(i2, 2);//拍照
                        break;
                }
            }
        }).show();
    }
       /* });*/
    //}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1://调取图库照片
                if (null != data) {
                    Uri uri = data.getData();
                    //根据需要，也可以加上Option这个参数
                    InputStream inputStream = null;
                    try {
                        inputStream = getContentResolver().openInputStream(uri);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    // mIcon.setImageBitmap(bitmap);
                }
                Intent i = new Intent();
                i.setAction("com.android.camera.action.CROP");
                i.setDataAndType(Uri.fromFile(mFile), "image/*");
                i.putExtra("crop", true);
                //框
                i.putExtra("aspectX", 1);
                i.putExtra("aspectY", 1);
                //大小
                i.putExtra("outputX", 200);
                i.putExtra("outputY", 200);
                i.putExtra("return-data", true);
                startActivityForResult(i, 222);

                break;
            case 2://拍照

//                Bundle bundle = data.getExtras();
//                Bitmap bitmap = (Bitmap) bundle.get("data");
//                mIcon.setImageBitmap(bitmap);


                BitmapFactory.Options opts = new BitmapFactory.Options();
                //只是解析图片的四周  不会加载图片，会加载图片的宽高
                opts.inJustDecodeBounds = true;
                //第一次获得宽高比例
                Bitmap bitmap = BitmapFactory.decodeFile(mFile.getPath(), opts);
                //根据宽高设置
                int w = opts.outWidth;
                int h = opts.outHeight;
                int width = 200;
                //缩放比例
                int ss = w / width;
                //缩放比
                opts.inSampleSize = ss;//  以1/16来缩放。
                //如果为true则没有图片
                opts.inJustDecodeBounds = false;
                //二次采样
                bitmap = BitmapFactory.decodeFile(mFile.getPath(), opts);


//                mIcon.setImageBitmap(bitmap);

                //裁剪
                Intent ii = new Intent();
                ii.setAction("com.android.camera.action.CROP");
                ii.setDataAndType(Uri.fromFile(mFile), "image/*");
                ii.putExtra("crop", true);
                //框
                ii.putExtra("aspectX", 1);
                ii.putExtra("aspectY", 1);
                //大小
                ii.putExtra("outputX", 200);
                ii.putExtra("outputY", 200);
                ii.putExtra("return-data", true);
                startActivityForResult(ii, 222);

                break;
            case 222:
                Bitmap bmp = data.getParcelableExtra("data");
                //   mIcon.setImageBitmap(bmp);
                break;

        }
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_photo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
